﻿using Net.Helper;

namespace Net.Share
{
    /// <summary>
    /// 线程组帧率信息类
    /// </summary>
    public struct FrameRateEntity
    {
        /// <summary>
        /// 线程组Id
        /// </summary>
        public int Id;
        /// <summary>
        /// 线程组帧率
        /// </summary>
        public int FrameRate;

        /// <summary>
        /// 线程组帧率信息构造
        /// </summary>
        /// <param name="id"></param>
        /// <param name="fps"></param>
        public FrameRateEntity(int id, int fps)
        {
            Id = id;
            FrameRate = fps;
        }
    }

    /// <summary>
    /// 表示网络数据流统计信息的类。
    /// 该类用于统计网络数据的发送、接收、解析等操作的频率和总量。
    /// </summary>
    public class NetworkTrafficStatistics
    {
        /// <summary>
        /// 每秒发送的数据包数量。
        /// </summary>
        public int SentPacketsPerSecond;
        /// <summary>
        /// 每秒发送的字节总数。
        /// </summary>
        public int SentBytesPerSecond;
        /// <summary>
        /// 每秒接收到的数据包数量。
        /// </summary>
        public int ReceivedPacketsPerSecond;
        /// <summary>
        /// 每秒接收到的字节总数。
        /// </summary>
        public int ReceivedBytesPerSecond;
        /// <summary>
        /// 每秒解析的RPC请求次数。
        /// </summary>
        public int RpcResolveCount;
        /// <summary>
        /// 从启动以来发送的总字节数。
        /// </summary>
        public long TotalSentBytes;
        /// <summary>
        /// 从启动以来接收的总字节数。
        /// </summary>
        public long TotalReceivedBytes;
        /// <summary>
        /// 客户端帧率（Frames Per Second）。
        /// 仅适用于客户端环境。
        /// </summary>
        public int FrameRate;
        /// <summary>
        /// 服务器端所有网络线程组的帧率统计信息。
        /// 仅适用于服务器环境。
        /// </summary>
        public FrameRateEntity[] FrameRates;

        /// <summary>
        /// 将所有线程组的帧率信息格式化为字符串。
        /// </summary>
        /// <returns>格式化的帧率信息字符串。</returns>
        public string FormatFrameRates()
        {
            var text = string.Empty;
            if (FrameRates == null)
                return text;
            for (int i = 0; i < FrameRates.Length; i++)
                text += $"FPS{FrameRates[i].Id}:{FrameRates[i].FrameRate} ";
            return text;
        }

        /// <inheritdoc/>
        public override string ToString()
        {
            return SpecifyFormat(includeAllDetails: true);
        }

        /// <summary>
        /// 将统计信息格式化为字符串。
        /// </summary>
        /// <param name="includeAllDetails">是否包含所有详细信息。</param>
        /// <param name="otherInformation"></param>
        /// <returns>格式化的统计信息字符串。</returns>
        public string SpecifyFormat(bool includeAllDetails = false, string otherInformation = null)
        {
            return $"流出:{SentPacketsPerSecond}次/{ByteHelper.ToString(SentBytesPerSecond)} " +
                $"流入:{ReceivedPacketsPerSecond}次/{ByteHelper.ToString(ReceivedBytesPerSecond)} " +
                $"FPS:{FrameRate} 解析:{RpcResolveCount}次 " +
                $"总流入:{ByteHelper.ToString(TotalReceivedBytes)} 总流出:{ByteHelper.ToString(TotalSentBytes)} {otherInformation} {(includeAllDetails ? FormatFrameRates() : string.Empty)}";
        }
    }
}